<template>
  <div>
    <avue-crud
        :option="option"
        :data="data"
        :table-loading="option.loading"
        :permission="permissionList"
        v-model:page="page"
        @row-save="save"
        @row-del="del"
        @row-update="update"
        @on-load="init"
        @search-change="search"
        @refresh-change="refresh"
        @selection-change="selectChange"
        v-model="form"
    >
      <template #menu="{ size, row, index }">
        <el-button
            @click="build(row)"
            icon="el-icon-pointer"
            text
            type="primary"
            :size="size"
        >生成
        </el-button
        >
      </template>
      <template #menu-left="{ size }">
        <el-button
            type="danger"
            :size="size"
            icon="el-icon-delete"
            v-if="!this.validatenull(this.permission.SysGenerate_remove)"
            @click="delBatch"
        >批量删除
        </el-button>

        <el-button
            type="success"
            :size="size"
            icon="el-icon-download"
            @click="importTable"
        >导 入
        </el-button>
      </template>
    </avue-crud>
    <el-dialog
        title="表信息"
        v-model="tableVisible"
        width="50%">
      <el-form :inline="true" :model="tableForm">
        <el-form-item label="数据库名">
          <el-input v-model="tableForm.tableSchema" placeholder="请输入数据库名" clearable/>
        </el-form-item>
        <el-form-item label="表名">
          <el-input v-model="tableForm.tableName" placeholder="请输入表名" clearable/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="importTable">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="tableSchema" label="数据库"/>
        <el-table-column prop="tableName" label="表名"/>
        <el-table-column prop="tableComment" label="表备注"/>
        <el-table-column prop="createTime" label="创建时间"/>
        <el-table-column prop="engine" label="引擎"/>
        <el-table-column fixed="right" label="操作栏">
          <template #default="scope">
            <el-button
                link
                type="primary"
                :size="scope.size"
                @click.prevent="importTableSave(scope.row)"
            >导  入
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="prev, pager, next" @change="importTable()" :total="tablePage.total"
                     v-model:page-size="tablePage.pageSize" v-model:current-page="tablePage.currentPage"/>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="tableVisible = false">取消</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import {
  build,
  list,
  pageList,
  query,
  save,
  update,
  remove,
  tablePage
} from "@/api/admin/SysGenerateApi.js";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      tableForm: {},
      tablePage: {
        total: 0,
        pageSize: 10,
        currentPage: 1
      },
      tableData: [],
      tableVisible: false,
      data: [],
      form: {},
      params: {},
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
      selectionList: [],
      option: {
        loading: true,
        selection: true,
        searchMenuSpan: 4,
        height: "auto",
        index: true,
        stripe: true,
        border: true,
        viewBtn: this.permission?.generate_add,
        column: {
          tablePrefix: {
            label: "表前缀",
          },
          menuId: {
            label: "菜单",
            type: "tree",
            props: {
              label: "name",
              value: "id",
            },
            dicUrl: `/admin/sysMenu/menuTree`,
          },
          tableName: {
            label: "表名",
            rules: [
              {
                required: true,
                message: "请输入表名",
                trigger: "blur",
              },
            ],
          },
          packageName: {
            label: "包名",
            rules: [
              {
                required: true,
                message: "请输入包名",
                trigger: "blur",
              },
            ],
          },
          module: {
            label: "模块名称	",
            rules: [
              {
                required: true,
                message: "请输入模块名称	",
                trigger: "blur",
              },
            ],
          },
          parent: {
            label: "继承公共",
            type: "radio",
            dicData: [
              {label: "是", value: true},
              {label: "否", value: false},
            ],
            rules: [
              {
                required: true,
                message: "请输入继承公共",
                trigger: "blur",
              },
            ],
          },
        },
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    selectIds() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id + "");
      });
      return ids.join(",") + "";
    },
    permissionList() {
      return {
        addBtn: !this.validatenull(this.permission.SysGenerate_add),
        delBtn: !this.validatenull(this.permission.SysGenerate_remove),
        editBtn: !this.validatenull(this.permission.SysGenerate_edit),
      };
    },
  },
  methods: {
    importTableSave(row){
      const data={
        tableName:row.tableName
      }
      save(data).then((res) => {
        this.tableVisible = false
        this.refreshLoad(res);
      });
    },
    importTable() {
      tablePage(this.tablePage, this.tableForm).then(res => {
        this.tableData = res.data.data.records
        this.tablePage.total = res.data.data.total
        this.tableVisible = true
      })
    },
    build(data) {
      this.$confirm("您确定生成代码吗？", "提示", {
        type: "warning",
      }).then((res) => {
        build({id: data.id}).then(res=>{
          if(res.data.code===200){
            this.$message.success("生成成功")
          }
        })
      }).catch((e) => {
      });
    },
    init() {
      this.option.loading = true;
      pageList(this.page, this.params)
          .then((res) => {
            this.data = res.data.data.records;
            this.page.total = res.data.data.total
            this.option.loading = false;
          })
          .catch(() => {
            this.option.loading = false;
          });
    },
    save(row, done, loading) {
      loading();
      save(row).then((res) => {
        this.refreshLoad(res);
      });
      done();
    },
    update(row, index, done, loading) {
      loading();
      update(row).then((res) => {
        this.refreshLoad(res);
      });
      done();
    },
    del(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            return remove(row.id);
          })
          .then((res) => {
            this.refreshLoad(res);
          });
    },
    delBatch() {
      if (this.selectIds.length === 0) {
        this.$message.error("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            return remove(this.selectIds);
          })
          .then((res) => {
            this.refreshLoad(res);
          });
    },
    refreshLoad(res) {
      this.init();
      this.$message({
        type: "success",
        message: res.data.msg,
      });
    },
    refresh() {
      this.init();
    },
    search(form, done) {
      this.params = form;
      this.init();
      done();
    },
    selectChange(list) {
      this.selectionList = list;
    },
  },
};
</script>
<style scoped lang="scss"></style>
